Ext.namespace('SLM');

SLM.Event = function( config ) {

	if ( typeof(config) == 'undefined') config = {};
	config.title = (config.cmd == 'add') ? "Add Event" : "Edit Event";
	config.iconCls = (config.cmd == 'add') ? "icon_events_add" : "icon_events_edit";

	this.store_event_type = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({url: Config.baseUrl + 'resources/api/app.php'})
		,	baseParams: { cmd: "eventtype-getlist", field: 'title' }
		, 	reader: new Ext.data.JsonReader({id: 'name', root: 'records'}, [ {name: 'name', mapping: 'value'}, 'event_type_id' ])
		,	autoLoad: true
	});
	
	this.store_land = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({url: Config.baseUrl + 'resources/api/app.php'})
		,	baseParams: { cmd: "land-getlist", field: 'title' }
		, 	reader: new Ext.data.JsonReader({id: 'name', root: 'records'}, [ {name: 'name', mapping: 'value'}, 'land_id' ])
		,	autoLoad: true
	});
	
	Ext.apply(this, config, {
			width: 390
		,	height: 320
		,	modal: true
		,	resizable: false
		,	layout: 'fit'
		, 	defaultButton: 'event_title'		
		,	onEsc: Ext.emptyFn
		,	autoScroll: true
		,	bodyStyle: 'background-color: #ff0000;'
		,	listeners: {
				show: function(a, b, c) {
					if (config.cmd == 'edit') {
						var filter = {
							items:[{	
									field: 'event_id'
								,	condition: 'EQUAL'
								,	value: config.data.event_id
							}]
						}
						this.items.first().getForm().load({
								params: { 
									cmd: 'event-list'
								,	filter: Ext.encode(filter)
								}
							,	success: function(a, response) {
								}
							,	failure: function(f, response) {
									if (response.result.success) {
										f.setValues( response.result.records[0] );
									}
								}
						});
					}
				}
			}
		,	items: new Ext.FormPanel({
					labelWidth: 75
				,	url: Config.baseUrl + 'resources/api/app.php'
				,	border: false
				,	defaultType: 'textfield'
				,	bodyStyle: 'padding: 10px;'
				,	defaults: {
						anchor: '100%'
					}
				,	items: [{
							xtype: 'hidden'
						,	name: 'event_id'
						,	value: 0
					}, {
							fieldLabel: 'Title'
						,	id: 'event_title'
						,	name: 'title'
						,	allowBlank: false
					}, {
							xtype: 'xtwincombo'
						,	allowBlank: false
						, 	fieldLabel: 'Event Type'
						, 	name: 'event_type_id'
						,	queryParam: 'value'
						,	store: this.store_event_type
						,	displayField: 'name'
						,	typeAhead: false
						, 	triggerAction: 'all'
						,	editable: false
						,	value:''
						,	valueField: 'event_type_id'
						, 	hiddenName: 'event_type_id'
					}, {
							xtype: 'xtwincombo'
						,	allowBlank: false
						, 	fieldLabel: 'Property'
						, 	name: 'land_id'
						,	queryParam: 'value'
						,	store: this.store_land
						,	displayField: 'name'
						,	typeAhead: false
						, 	triggerAction: 'all'
						,	editable: false
						,	value:''
						,	valueField: 'land_id'
						, 	hiddenName: 'land_id'
					}, {
							fieldLabel: 'Start Date'
						,	format: 'Y-m-d'
						,	name: 'date_start'
						,	ref: '../refStartDate'
						,	xtype: 'xtwincombodate'		
						,	hideTrigger1: false
					}, {
							fieldLabel: 'End Date'
						,	format: 'Y-m-d'
						,	name: 'date_end'
						,	ref: '../refEndDate'
						,	xtype: 'xtwincombodate'	
						,	hideTrigger1: false
					}, {
							fieldLabel: 'Description'
						,	name: 'description'
						,	xtype: 'textarea'
						,	height: 100	
					}]
			})
		,	buttons: [{
				text: 'Save'
			,	scope: this
			,	handler: function() {
					this.items.first().getForm().submit({ 
							params: { 
									cmd: 'event-update'
							}
						,	scope: this
						,	success: function( form, r ) {
								if ( r.result.success ) {
									this.fireEvent('saveSuccessful');
									this.close();
								}
							}
					});
				}
			},{
				text: 'Cancel'
			,	scope: this	
			,	handler: function(){
					this.close();
				}
			}]
	});

	SLM.Event.superclass.constructor.apply(this, arguments);

};

Ext.extend( SLM.Event, Ext.Window, {});